<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

/**
 * 前台公共库文件
 * 主要定义前台公共函数库
 */

/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function check_verify($code, $id = 1){
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}

/**
 * 获取列表总行数
 * @param  string  $category 分类ID
 * @param  integer $status   数据状态
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_list_count($category, $status = 1){
    static $count;
    if(!isset($count[$category])){
        $count[$category] = D('Document')->listCount($category, $status);
    }
    return $count[$category];
}

/**
 * 获取段落总数
 * @param  string $id 文档ID
 * @return integer    段落总数
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_part_count($id){
    static $count;
    if(!isset($count[$id])){
        $count[$id] = D('Document')->partCount($id);
    }
    return $count[$id];
}

/**
 * 获取导航URL
 * @param  string $url 导航URL
 * @return string      解析或的url
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_nav_url($url){
    switch ($url) {
        case 'http://' === substr($url, 0, 7):
        case '#' === substr($url, 0, 1):
            break;        
        default:
            $url = U($url);
            break;
    }
    return $url;
}

/**
 * 获取内容所有图片
 * @param $content
 * @param string $order
 * @return string
 */
function getImgs($content,$order='ALL'){
    $pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
    preg_match_all($pattern,$content,$match);
    if(isset($match[1])&&!empty($match[1])){
        if($order==='ALL'){
            return $match[1];
        }
        if(is_numeric($order)&&isset($match[1][$order])){
            return $match[1][$order];
        }
    }
    return '';
}

/**
 * 计算折扣
 * @param $zhekou
 * @return int
 */
function count_zhekou($zhekou){
    return intval($zhekou) * 0.4;
}

/**
 * 购物车数量
 * @param $wechat_id
 * @return mixed
 */
function cartCount(){
    $data = M('cart')->alias('c')->field('sum(num) as count')->where(array('is_create' => 0, 'client_id' => $_SESSION['userid']))->find();
    $count = intval($data['count']);
    if($count > 0){
        return '<span class="ui-badge-cornernum">'. $count .'</span>';
    }else{
        return '';
    }
}

/**
 * 商品收藏
 * @param $goods_id
 * @return mixed
 */
function getCollect($goods_id){
    $count = M('collect')->where(array('goods_id' => $goods_id, 'cid' => $_SESSION['userid']))->count();
    $html = '';
    if($count > 0){
        $html = '<a class="collect tc selected" href="javascript::"><i class="iconfont icon-likefill"></i><span>已收藏</span></a>';
    }else{
        $html = '<a class="collect tc" href="javascript::"><i class="iconfont icon-likefill"></i><span>收藏</span></a>';
    }
    return $html;
}

function getCustomPhone(){
    return M('clients_address')->where(array('type' => 1, 'class_id' => $_SESSION['class_id']))->getField('phone');
}

/**
 * 生成订单号
 * @return string
 */
function build_order_no($userid = ''){
    return $userid . '-' .date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}

/**
 * 获取单位名
 * @param $id
 * @return mixed
 */
function getUnitName($id){
    return M('unit')->where(array('id' => $id))->getField('name');
}

function getShopPhone($class_id){
    return  M('clients_address')->where(array('class_id' => $class_id))->getField('phone');
}
